/**
 * Created by Mr.Xing on 2018/10/19.
 */
mini.parse();
function SetDbData(data){
    console.log(data);//'edit-db',id,properties
    //跨页面传递的数据对象，克隆后才可以安全使用
    data = mini.clone(data);
    if (data.action == "edit-db" && data.properties){
        for(var i=0;i<data.properties.length;i++){
            if(data.properties[i].cTableField = data.id) {
                setForm(data.properties[i].dbSourse);
            }
        }
    }
}

var dbData = {};
function onDbDriver(){
    //数据库连接驱动
    var dbDriver = mini.get("dbDriver").getValue();
    //数据连接地址
    var dbUrl = mini.get("dbUrl").getValue();
    //连接username
    var username = mini.get("username").getValue();
    //连接password
    var password = mini.get("password").getValue();
    //需要执行的sql语句
    var dbSql = mini.get("dbSql").getValue();

    if(dbDriver == ""){
        mini.alert("数据库驱动不可为空");
    } else if(dbUrl == ""){
        mini.alert("链接地址不可为空");
    } else if(username == ""){
        mini.alert("username不可为空");
    } else if(password == ""){
        mini.alert("password不可为空");
    } else if(dbSql == ""){
        mini.alert("执行sql不可为空");
    } else{
        //ajax请求url地址
        var url = ajaxUrl + "/dbsource/list.do";
        $.ajax({
            type : 'POST',
            url : url,
            data : {
                dbDriver : dbDriver,
                dbUrl : dbUrl,
                username : username,
                password : password,
                dbSql : dbSql
            },
            dataType : 'json',
            success : function (data){
                dbData = getForm();
                console.log(dbData);
                CloseWindow("ok");
            },
            error : function(){
                mini.alert("请求失败");
            }
        })
    }
}

function onDBData() {
    return dbData;
}
//确定按钮
function onOk(e) {
    onDbDriver();
}
//取消按钮
function onCancel(e) {
    CloseWindow("cancel");
}

function CloseWindow(action) {
    if (window.CloseOwnerWindow)
        return window.CloseOwnerWindow(action);
    else
        window.close();
}

function getForm() {
    var form = new mini.Form("#dbSource");
    var data = form.getData(true, false);
    // var s = mini.encode(data);
    return data;
    // alert(s);
    //form.setIsValid(false);
}
function setForm(obj) {
    var form = new mini.Form("#dbSource");
    form.setData(obj, false);
}